package com.hxx.controller;

import com.hxx.domain.Dish;
import com.hxx.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dishes")
@CrossOrigin
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping
    public List<Dish> getAllDishes() {
        return dishService.findByAvailableTrue();
    }

    /**
     * 根据ID获取菜品信息
     * @param id
     * @return
     * ResponseEntity是用于构建HTTP响应的类
     */
    @GetMapping("/{id}")
    public ResponseEntity<Dish> getDish(@PathVariable int id) {
        Dish dish = dishService.getById(id);
        if (dish == null){
            //return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            return ResponseEntity.notFound().build();
        }else {
            //return new ResponseEntity<>(dish, HttpStatus.OK);
            // 返回菜品信息
            return ResponseEntity.ok(dish);
        }
    }

    /**
     *
     * @param category
     * @return
     */
    @GetMapping("/category/{category}")
    public List<Dish> getDishesByCategory(@PathVariable String category) {
        return dishService.findByCategory(category);
    }
}
